<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="140px"
    >
      <el-form-item
        label="订单号"
        prop="orderSn"
      >
        <el-input
          v-model="dataForm.orderSn"
          required 
          placeholder="订单号"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="订单ID"
        prop="orderID"
      >
        <el-input
          v-model="dataForm.orderID"
          required 
          placeholder="订单ID"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="交易流水号"
        prop="alipayTradeNo"
      >
        <el-input
          v-model="dataForm.alipayTradeNo"
          required 
          placeholder="交易流水号"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="支付总金额"
        prop="totalAmount"
      >
        <el-input
          v-model="dataForm.totalAmount"
          required 
          placeholder="支付总金额"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="交易内容"
        prop="subjcet"
      >
        <el-input
          v-model="dataForm.subjcet"
          required 
          placeholder="交易内容"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="支付状态"
        prop="status"
      >
        <el-switch
          v-model="dataForm.status"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
        ></el-switch>
      </el-form-item>
    </el-form>
    <span
      slot="footer"
      class="dialog-footer"
    >
      <el-button @click="visible = false">取消</el-button>
      <el-button
        type="primary"
        @click="dataFormSubmit()"
      >确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        orderSn: '',
        orderID: 0,
        alipayTradeNo: '',
        totalAmount: 0,
        subjcet: '',
        status: 1,
        createTime: '',
        confirmTime: '',
        newTime: ''
      },
      dataRule: {
        orderSn: [{ required: true, message: '订单号不能为空', trigger: 'blur' }],
        orderID: [
          { required: true, message: '订单ID不能为空', trigger: 'blur' }
        ],
        alipayTradeNo: [
          { required: true, message: '支付流水号不能为空', trigger: 'blur' }
        ],
        totalAmount: [
          { required: false, message: '支付总金额不能为空', trigger: 'blur' }
        ],
        subjcet: [
          { required: true, message: '支付内容不能为空', trigger: 'blur' }
        ],
        status: [
          {
            required: true,
            message: '支付状态[0-未支付；1-已支付]不能为空',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    // getCurrentTime () {
    //   this.newTime = new Date()
    //   console.log(this.newTime)
    // },
    init (id) {
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl('/sys/role/list'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({data}) => {
        this.roleList = data.page.list
      }).then(() => {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
      }).then(() => {
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/order/paymentinfo/info/${this.dataForm.id.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.orderSn = data.paymentInfo.orderSn
              this.dataForm.orderID = data.paymentInfo.orderID
              this.dataForm.alipayTradeNo = data.paymentInfo.alipayTradeNo
              this.dataForm.totalAmount = data.paymentInfo.totalAmount
              this.dataForm.subjcet = data.paymentInfo.subjcet
              this.dataForm.status = data.paymentInfo.status
              this.dataForm.createTime = data.paymentInfo.createTime
              this.dataForm.confirmTime = data.paymentInfo.confirmTime
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      // this.$options.methods.getCurrentTime()
      this.newTime = new Date()
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/order/paymentinfo/${!this.dataForm.id ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id.id || undefined,
              'orderSn': this.dataForm.orderSn,
              'orderID': this.dataForm.orderID,
              'alipayTradeNo': this.dataForm.alipayTradeNo,
              'totalAmount': this.dataForm.totalAmount,
              'subjcet': this.dataForm.subjcet,
              'status': this.dataForm.status,
              'createTime': !this.dataForm.id ? this.newTime : this.dataForm.createTime,
              'confirmTime': this.dataForm.confirmTime
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
